package com.sgb.kjwl.viewmodel.pick;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.repository.PickProjectRepertory;
import com.sgb.kjwl.view.ui.fragment.pick.PickPickingListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PickPickingListFragModel extends BaseViewModel {
    private PickProjectRepertory repertory;
    private PickPickingListFragment fragment;
    private int page = 1;
    private int size = 10;
    public int total;

    public PickPickingListFragModel(PickPickingListFragment fragment, PickProjectRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getData(String no){
        page =1;
        Map map = new HashMap();
        String[] companyNoList = {no};
        map.put("limit",10);
        map.put("page",page);
        map.put("companyNoList",companyNoList);
        APIManager.getInstance().getPickAPI().getTenderList(map).enqueue(new Callback<BaseEntity<List<TenderListEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TenderListEntity>>> call, Response<BaseEntity<List<TenderListEntity>>> response) {
                if (response.body()==null || response.body().getState() == null){
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    fragment.showData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TenderListEntity>>> call, Throwable t) {

            }
        });

    }

    public void getMoreData(String no){
        page++;
        Map map = new HashMap();
        String[] companyNoList = {no};
        map.put("limit",10);
        map.put("page",page);
        map.put("companyNoList",companyNoList);
        APIManager.getInstance().getPickAPI().getTenderList(map).enqueue(new Callback<BaseEntity<List<TenderListEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TenderListEntity>>> call, Response<BaseEntity<List<TenderListEntity>>> response) {
                if (response.body()==null || response.body().getState() == null){
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)){
                    fragment.showMoreData(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TenderListEntity>>> call, Throwable t) {

            }
        });
    }

    public void getUrlSettingTender(String id) {
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "加载中", 0, true, null, false);
        APIManager.getInstance().getPickAPI().getUrlSetting("FrontDeskBidding").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    fragment.getUrl(url, id);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }
}
